package com.tinem.platform.module.starter.web.config;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.tinem.platform.module.starter.web.filter.ChannelDatasourceFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className ChannelDatasourceConfiguration
 * @description 渠道数据源配置
 * @site
 * @company
 * @create 2021/12/7 19:59:49
 */
@Slf4j
@Configuration
public class ChannelDatasourceConfiguration {

    @Value("${spring.profiles.active}")
    String active;

    @Bean
    @ConditionalOnClass(DynamicDataSourceContextHolder.class)
    public FilterRegistrationBean channelDatasourceFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setOrder(3);
        filterRegistrationBean.setFilter(new ChannelDatasourceFilter(active));
        filterRegistrationBean.setName("ChannelDatasourceFilter");
        filterRegistrationBean.addUrlPatterns("*.do","*.api","*.notify");
        return filterRegistrationBean;
    }

}
